package fi.helsinki.cs.ohtu10k.view;

import java.awt.Dimension;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.JComboBox;
import org.joda.time.DateTime;

/**
 * Class implementing a simple graphical time selector using JComboBoxes. 
 * @author Thomas Nyman
 *
 */
@SuppressWarnings("serial")
public class TimeSelector extends JPanel {
	private JComboBox selectorHour;
	private JComboBox selectorMinute;
	
	/**
	 * Creates a new TimeSelector.
	 */
	public TimeSelector() {
		selectorHour = new JComboBox(Constants.HOURS);
		selectorMinute = new JComboBox(Constants.MINUTES);
		
		setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
		add(selectorHour);
		add(Box.createRigidArea(new Dimension(10, 0)));
		add(selectorMinute);
		add(Box.createHorizontalGlue());
	}
	
	/**
	 * Returns DateTime with selected time set to specified date.
	 * @param date DateTime which time to set.
	 * @return DateTime with selected time set to specified date.
	 */
	public DateTime getTimeAtDate(DateTime date) {
		return date.withHourOfDay(selectorHour.getSelectedIndex()+ApplicationWindow.STARTING_HOUR).
					withMinuteOfHour(selectorMinute.getSelectedIndex()*15);
	}
	
	/**
	 * Enables the combo box so that items can be selected. 
	 * When the combo box is disabled, items cannot be selected and 
	 * values cannot be typed into its field (if it is editable).
	 * @param b a boolean value, where true enables the component and false disables it
	 */
	@Override
	public void setEnabled(boolean b) {
		selectorHour.setEnabled(b);
		selectorMinute.setEnabled(b);
	}

}
